﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.IotInfo;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using DB = LJX.SmartPark.Domain.IotInfo.LightingInfo;

namespace LJX.SmartPark.Service.IotInfo.LightingInfo
{
    public class LightingInfoServeice : ILightingInfoServeice
    {
        private readonly IRepository<DB.LightingInfo> _lightingInfoServeice;

        public LightingInfoServeice(IRepository<DB.LightingInfo> lightingInfoServeice)
        {
            _lightingInfoServeice = lightingInfoServeice;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> DeleteLightingInfoAsync(DB.LightingInfo input)
        {
            var result = await _lightingInfoServeice.DeleteByIdAsync(input.MapTo<DB.LightingInfo>());
            return Response<bool>.Ok(result > 0);
        }

        /// <summary>
        /// 获取总条数
        /// </summary>
        /// <returns></returns>
        public int GetCount()
        {
            return _lightingInfoServeice.GetCount();
        }

        /// <summary>
        /// 查询分页
        /// </summary>
        /// <param name="fenye"></param>
        /// <returns></returns>
        public async Task<Response<List<DB.LightingInfo>>> GetLightingInfoAsync(Fen fen)
        {
            var lightinginfo = await _lightingInfoServeice.GetAllListAsync();
            var result = lightinginfo.MapToList<DB.LightingInfo>();

            if (!string.IsNullOrWhiteSpace(fen.status))
            {
                result = result.Where(a => a.ConnectionStatus == bool.Parse(fen.status)).ToList();
            }

            var totalCount = result.Count();

            result = result.Skip(((fen.pageIndex - 1) * fen.pageSize)).Take(fen.pageSize).ToList();

            return Response<List<DB.LightingInfo>>.Ok(result, totalCount);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Response<DB.LightingInfo> GetLightingInfoById(DB.LightingInfo input)
        {
            var lightinginfo = _lightingInfoServeice.GetListQuery().FirstOrDefault(m => m.Id == input.Id);
            var result = lightinginfo.MapTo<DB.LightingInfo>();
            return Response<DB.LightingInfo>.Ok(result);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> PostAddLightingInfoAsync(DB.LightingInfo input)
        {
            var result = _lightingInfoServeice.InsertAsyncById(input.MapTo<DB.LightingInfo>());

            return Response<bool>.Ok(result.Result > 0);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> PutUpdLightingInfoAsync(DB.LightingInfo input)
        {
            var result = _lightingInfoServeice.UpdateAsync(input.MapTo<DB.LightingInfo>());

            return Response<bool>.Ok(result.Result > 0);
        }

        /// <summary>
        /// 查询分页
        /// </summary>
        public class Fen
        {
            public int pageIndex { get; set; }
            public int pageSize { get; set; }
            public string? status { get; set; }
        }
    }
}
